package com.cgy.utils.base.strings.stream;

import com.cgy.utils.base.arrays.ArraysHelper;
import com.cgy.utils.base.list.ListHelper;
import com.cgy.utils.base.strings.StringHelper;
import com.cgy.utils.base.type.TypeHelper;

import java.util.List;
import java.util.Map;

/**
 * 流式处理
 * @author CHTK
 */
public class StringStreamBuilder {

    private String tempValue;
    private boolean isNull;
    private List<String> tempList;
    private Class<?> classes;

    public StringStreamBuilder(String tempValue) {
        this.tempValue = tempValue;
        this.tempList = ListHelper.newArrayList(tempValue);
    }

    /**
     * 是否需要空格
     * @return
     */
    public StringStreamBuilder serializeNulls() {
        this.isNull = true;
        return this;
    }

    /**
     * 不为空时处理
     * @return
     */
    public StringStreamBuilder stringIfNotBlank(final StringHandler stringHandler) {
        if(StringHelper.isNotBlank(this.tempValue) && null != stringHandler) {
            this.tempValue = stringHandler.execute(this.tempValue);
        }
        return this;
    }
    /**
     * 为空时处理
     * @return
     */
    public StringStreamBuilder stringElse(final StringHandler stringHandler) {
        if(StringHelper.isBlank(this.tempValue) && null != stringHandler) {
            this.tempValue = stringHandler.execute(this.tempValue);
        }
        return this;
    }
    /**
     *
     * @return
     */
    public StringStreamBuilder filter(StringFilter stringFilter) {
       this.tempValue =  null == stringFilter ? this.tempValue : stringFilter.filter(this.tempValue);
       return this;
    }

    /**
     * 占位符处理
     * @param params
     * @return
     */
    public StringStreamBuilder placeholder(final Map<String, Object> params, final String start, final String end) {
        this.tempValue = StringHelper.matcher(this.tempValue, params, start, end);
        return this;
    }
    /**
     * 替换
     * @param target
     * @param replacement
     * @return
     */
    public StringStreamBuilder replace(final String target, final String replacement) {
        this.tempValue = this.tempValue.replace(target, replacement);
        return this;
    }
    /**
     * 大写
     * @return
     */
    public StringStreamBuilder upperCase() {
        this.tempValue = this.tempValue.toUpperCase();
        return this;
    }
    /**
     * 小写
     * @return
     */
    public StringStreamBuilder lowerCase() {
        this.tempValue = this.tempValue.toLowerCase();
        return this;
    }

    /**
     * 初始化
     * @return
     */
    public StringStreamBuilder splitter(final String separator) {
        if(StringHelper.isNotBlank(tempValue)) {
            String[] split = null;
            if(isNull) {
                split = tempValue.split(separator, -1);
            } else {
                split = tempValue.split(separator);
            }
            this.tempList = ArraysHelper.newArrays(split);
        }
        return this;
    }

    /**
     *
     * @return
     */
    public boolean isNull() {
        return null == this.tempValue;
    }
    /**
     *
     * @return
     */
    public List<String> toList() {
        return this.tempList;
    }
    /**
     *
     * @return
     */
    @Override
    public String toString() {
        return this.tempValue;
    }
    /**
     *
     * @return
     */
    public <T>T toEntity(Class<T> classes) {
        return TypeHelper.conversion(this.tempValue, classes);
    }

    /**
     * 过滤器
     */
    public static interface StringFilter {
        /**
         *
         * @param value
         * @return
         */
        public String filter(String value);
    }
    /**
     * 如果不为空
     */
    public static interface StringHandler {
        /**
         *
         * @param value
         * @return
         */
        public String execute(String value);
    }

}
